public class UnsafeThreadDemo {

    private static int counter = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(new IncrementCounter());
        Thread thread2 = new Thread(new IncrementCounter());

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Counter: " + counter);
    }

    static class IncrementCounter implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 100000; i++) {
                counter++;
            }
        }
    }
}


//class Test {
//    public synchronized void test() {
//
//    }
//}
////等价于
//class Test {
//    public void test() {
//        synchronized (this) {
//
//        }
//    }
//}
//class Test {
//    public synchronized static void test() {
//
//    }
//}
////等价于
//class Test {
//    public static void test() {
//        synchronized (Test.class) {
//
//        }
//    }
//}